package com.gm.wholesale.bluetooth.weightBlue;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import com.gm.wholesale.bluetooth.BluetoothUtil;
import com.gm.wholesale.bluetooth.service.BluetoothService;
import com.youth.banner.util.LogUtils;

public class KHWeightBluetooth extends BaseWeightBluetooth{
    private static KHWeightBluetooth mKHWeightBluetooth;
    SCALENOW scalenow = new SCALENOW();
    Boolean b_scaleIsConnect = Boolean.FALSE;
    final Boolean SendMoudle = true;  //  这个表示是连接称重模块
    public static KHWeightBluetooth getBluetoothManger(Context context) {
        if (mKHWeightBluetooth == null) {
            mKHWeightBluetooth = new KHWeightBluetooth();
            mBluetoothUtil=BluetoothUtil.newInstance();
            mKHWeightBluetooth.initWeightBluetoothManger(context);
        }
        return mKHWeightBluetooth;
    }
    public KHWeightBluetooth initWeightBluetoothManger(Context context) {
        mContext = context;
        mHandler = new Handler() {
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 1:
                        Bundle bundle = msg.getData();
                        String data = new String(bundle.getByteArray("weight"));
                        GetWeight(bundle.getByteArray("weight"));
                        if (!TextUtils.isEmpty(data)
                                && data.startsWith("ST")) { //ST开头稳定数据
                            mWeightBean.setWeight(scalenow.sformatNetWeight);
                            if (!TextUtils.isEmpty(scalenow.sUnit)) {
                                mWeightBean.setsUnit(scalenow.sUnit.trim());
                            }
                            if ( mListener!= null) {
                                mListener.getNumber(mWeightBean);
                            }
                        }
                        break;
                    case 2:
                        if (msg.arg1 < 2) {
                            LogUtils.d("连接失败");
                            b_scaleIsConnect = Boolean.FALSE;
                            if (mListener != null) {
                                mListener.connectionFail();
                            }
                        } else {
                            if (mListener != null) {
                                if (mBluetoothService!=null){
                                    mListener.connection(mBluetoothService.getConnectionDevice(context));
                                }
                            }
                            b_scaleIsConnect = Boolean.TRUE;
                            if (SendMoudle) {
                                LogUtils.d("已连接");
                                //模块需要发送RN1表示连续读净重
                                write("RN1\r\n".getBytes());
                            }
                        }
                }
            }
        };

        mBluetoothService= new BluetoothService(mContext, mHandler);
        mBluetoothService.setIonHandelDataListener((readByteSize, buffer, handler) -> {
            byte[] strdata = new byte[50];
            int pStrdata = 0;
            int r[] = new int[4];
            int ad;
            for (int i = 0; i < readByteSize; i++) {
                if (buffer[i] == 0x2) pStrdata = 0;
                else if (buffer[i] == 0x0A) {
                    if (pStrdata > 0) {
                        if (buffer[pStrdata - 1] == 0x0d) pStrdata--;
                        strdata[pStrdata] = 0;    //��ʾ����
                        Bundle b = new Bundle();
                        b.putByteArray("weight", strdata);
                        Message message = new Message();
                        message.what = 1;
                        message.setData(b);
                        mHandler.sendMessage(message);
                    }

                    pStrdata = 0;
                } else {
                    strdata[pStrdata] = buffer[i];
                    if (++pStrdata >= 50) pStrdata = 0;
                }


            }
        });
        return mKHWeightBluetooth;
    }




  public   void GetWeight(byte[] databuf) {

        int i, j, offset = 6;
        String ShowFmtTab[] = {"%1$8.0f", "%1$8.1f", "%1$8.2f", "%1$8.3f", "%1$8.4f", "%1$8.5f", "%1$8.6f", "%1$8.6f", "%1$8.6f"};
        boolean StartFalg = false;
        scalenow.bZeroFlag = true;
        scalenow.bOverFlag = false;
        scalenow.bWeiStaFlag = false;
        switch (databuf[0]) {
            case 'o':
            case 'O':
                scalenow.bOverFlag = true;
                break;
            case 'u':
            case 'U':
                scalenow.bWeiStaFlag = false;
                offset = 6;    //6
                break;
            case 's':
            case 'S':
                scalenow.bWeiStaFlag = true;
                break;
        }
        if (databuf[5] == '-') offset = 5;
        for (i = 0; i < 14; i++) {
            if (databuf[i + offset] == '\'') databuf[i + offset] = '.';
            if (StartFalg) {
                if (((databuf[i + offset] > '9') || (databuf[i + offset] < '.')) && (!((databuf[i + offset] == ' ') && (databuf[i + offset + 1] <= '9')))) {
                    break;
                }
            } else if ((databuf[i + offset] >= '0') && (databuf[i + offset] <= '9')) {
                StartFalg = true;
                if (databuf[i + offset] != '0') scalenow.bZeroFlag = false;
            }
        }
        scalenow.sformatNetWeight = new String(databuf, offset, i);


        for (j = 0; j < 6; j++) {
            if (databuf[i + j + offset] < 0x20) {
                break;
            }
        }
        scalenow.sUnit = new String(databuf, i + offset, j);

    }

    public static class SCALENOW {
        public float formatNetWeight;
        public String sformatNetWeight = "0";
        public String sUnit = "0";
        public boolean bWeiStaFlag;
        public boolean bZeroFlag;
        public boolean bOverFlag;
    }


    public void destory() {
        super.destory();
        mBluetoothService=null;
        mKHWeightBluetooth = null;
        mHandler = null;

    }


}
